import Vue from 'vue'
import Router from 'vue-router'
import App from '../App'

const home = r => require.ensure([], () => r(require('@/page/home/home')), 'home');
const createMonitor = r => require.ensure([], () => r(require('@/page/monitoring/createMonitor')), 'createMonitor');
const addMonitor = r => require.ensure([], () => r(require('@/page/monitoring/addMonitor')), 'addMonitor');
const monitorList = r => require.ensure([], () => r(require('@/page/monitoring/monitorList')), 'monitorList');
const like = r => require.ensure([], () => r(require('@/page/monitoring/like')), 'like');
const keywordDetail = r => require.ensure([], () => r(require('@/page/monitoring/keywordDetail')), 'keywordDetail');

Router.prototype.goBack = function () {
  this.isBack = true;
  this.go(-1)
}

Vue.use(Router)

export default new Router({
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop;
      }
      return { x: 0, y: to.meta.savedPosition || 0 }
    }
  },
  routes: [
    {
      path: '/',
      component: App, //顶层路由，对应index.html
      children:[
        {
          path: '',
          redirect: '/home'
        },
        {
          path: '/home',
          component: home
        },
        {
          path: '/createMonitor',
          component: createMonitor,
        },
        {
          path: '/addMonitor',
          component: addMonitor,
        },
        {
          path: '/monitorList',
          component: monitorList,
        },
        {
          path: '/like',
          component: like,
        },
        {
          path: '/keywordDetail',
          component: keywordDetail,
        },
      ]
    }
  ]
})
